﻿(function ($) {

    var initialItem = function (target, settings) {
        var $target = $(target);
        var $btn_delete;
        var $btn_detail;
        var $btn_custom;
        if (settings.detailEvent) {
            $btn_detail = $('<div class="btn_detail"></div>').prependTo(target).hide();
            $btn_detail.css('width', settings.size).css('height', settings.size).css('float', "right");
            var $dalimg = $('<img></img>').appendTo($btn_detail).css('width', settings.size).css('height', settings.size);
            $dalimg.attr('src', '/images/106.png');
            $btn_detail.click(settings.detailEvent);
        }

        if (settings.deleteEvent) {
            $btn_delete = $('<div class="btn_delete"></div>').prependTo(target).hide();
            $btn_delete.css('width', settings.size).css('height', settings.size).css('float', "right");
            var $bakimg = $('<img></img>').appendTo($btn_delete).css('width', settings.size).css('height', settings.size);

            $bakimg.attr('src', '/images/onebit_33.png');
            $btn_delete.click(settings.deleteEvent);
        }

        if (settings.customEvent) {
            $btn_custom = $('<div class="btn_custom"></div>').prependTo(target).hide();
            $btn_custom.css('width', settings.size).css('height', settings.size).css('float', "right");
            var $cstimg = $('<img></img>').appendTo($btn_custom).css('width', settings.size).css('height', settings.size);
            $cstimg.attr('src', '/images/onebit_33.png');
            $btn_custom.click(settings.customEvent);
        }

        if (settings.valign == 'center') {
            var height = $target.outerHeight();
            if (settings.deleteEvent)
                $btn_delete.css('margin-top', height / 2 - $btn_delete.height() / 2);
            if (settings.detailEvent)
                $btn_detail.css('margin-top', height / 2 - $btn_detail.height() / 2);
            if (settings.customEvent)
                $btn_custom.css('margin-top', height / 2 - $btn_custom.height() / 2);
        }
        if (settings.valign == 'top') {
            if ($btn_delete)
                $btn_delete.css('margin-top', 0);
            if ($btn_detail)
                $btn_detail.css('margin-top', 0);
            if ($btn_custom)
                $btn_custom.css('margin-top', 0);
        }
        if (settings.showIcon == 'hover')
            $target.hover(
            function () {
                if (settings.deleteEvent)
                    $btn_delete.animate({ opacity: 'show' }, 'fast');
                if (settings.detailEvent)
                    $btn_detail.animate({ opacity: 'show' }, 'fast');
                if (settings.customEvent)
                    $btn_custom.animate({ opacity: 'show' }, 'fast');
            },
            function () {
                if (settings.deleteEvent)
                    $btn_delete.animate({ opacity: 'hide' }, 'fast');
                if (settings.detailEvent)
                    $btn_detail.animate({ opacity: 'hide' }, 'fast');
                if (settings.customEvent)
                    $btn_custom.animate({ opacity: 'hide' }, 'fast');
            });
        else if (settings.showIcon == 'always') {
            if (settings.deleteEvent)
                $btn_delete.show();
            if (settings.detailEvent)
                $btn_detail.show();
            if (settings.customEvent)
                $btn_custom.show();
        }
    }

    jQuery.fn.addHoverFunctions = function (options) {
        settings = jQuery.extend({
            deleteEvent: null,
            detailEvent: null,
            customEvent: null,
            CustomBtnUrl: '/images/48.png',
            valign: 'center',
            size: '20px',
            showIcon: 'hover'
        }, options);
        initialItem(this, settings);
        return this;
    };

})(jQuery);


